using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace Microsoft.EntityFrameworkCore.Dm.Storage.Internal;

public class DmByteArrayTypeMapping : ByteArrayTypeMapping
{
	internal const int MaxSize = 32767;

	private readonly StoreTypePostfix? _storeTypePostfix;

	public DmByteArrayTypeMapping([NotNull] string storeType, DbType? dbType = System.Data.DbType.Binary, int? size = null, bool fixedLength = false, ValueComparer comparer = null, StoreTypePostfix? storeTypePostfix = null)
			: base(new RelationalTypeMapping.RelationalTypeMappingParameters(new CoreTypeMapping.CoreTypeMappingParameters(typeof(byte[]), null, comparer, null, null, null), storeType, DmByteArrayTypeMapping.GetStoreTypePostfix(storeTypePostfix, size), dbType, false, size, fixedLength, null, null))
	{
		this._storeTypePostfix = storeTypePostfix;
	}

	private static StoreTypePostfix GetStoreTypePostfix(StoreTypePostfix? storeTypePostfix, int? size)
	{
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		StoreTypePostfix? val = storeTypePostfix;
		if (!val.HasValue)
		{
			if (size.HasValue && size <= 32767)
			{
				return (StoreTypePostfix)1;
			}
			return (StoreTypePostfix)0;
		}
		return val.GetValueOrDefault();
	}

	protected DmByteArrayTypeMapping(RelationalTypeMappingParameters parameters)
		: base(parameters)
	{
	}//IL_0001: Unknown result type (might be due to invalid IL or missing references)


	private static int CalculateSize(int? size)
	{
		if (size != null)
		{
			int? num = size;
			int num2 = 0x7FFF;
			if ((num.GetValueOrDefault() < num2) & (num != null))
			{
				return size.Value;
			}
		}
		return 0x7FFF;
	}

	public override RelationalTypeMapping Clone(string storeType, int? size)
	{
		return new DmByteArrayTypeMapping(this.Parameters.WithStoreTypeAndSize(storeType, size, new StoreTypePostfix?(DmByteArrayTypeMapping.GetStoreTypePostfix(this._storeTypePostfix, size))));
	}

	// Token: 0x06000185 RID: 389 RVA: 0x000087F8 File Offset: 0x000069F8
	public override CoreTypeMapping Clone(ValueConverter converter)
	{
		return new DmByteArrayTypeMapping(this.Parameters.WithComposedConverter(converter));
	}

	protected override void ConfigureParameter(DbParameter parameter)
	{
		object value = parameter.Value;
		int? num = (value as byte[])?.Length;
		int num2 = CalculateSize(((RelationalTypeMapping)this).Size);
		parameter.Size = ((value == null || value == DBNull.Value || (num.HasValue && num <= num2)) ? num2 : (-1));
	}

	protected override string GenerateNonNullSqlLiteral(object value)
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("0x");
		byte[] array = (byte[])value;
		foreach (byte b in array)
		{
			stringBuilder.Append(b.ToString("X2", CultureInfo.InvariantCulture));
		}
		return stringBuilder.ToString();
	}
}
